package com.ayf.payment.game.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ayf.payment.game.api.auths.ApplicationContextProvider;
import com.ayf.payment.game.api.channel.BaseHandler;
import com.ayf.payment.game.api.service.ChannelService;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.stereotype.Service;


@Service
public class ChannelServiceImpl implements ChannelService {

    @Override
    public JSONObject orderCreate(JSONObject inputParam) {
        try {
            return getHandler(inputParam.getString("channelCode")).orderCreate(inputParam);
        } catch (ClassNotFoundException e) {
            LogPortal.error("{} orderCreate 驱动类找不到",inputParam.getString("traceId"));
        }
        return null;
    }

    @Override
    public JSONObject orderQuery(JSONObject inputParam) {
        try {
            return getHandler(inputParam.getString("channelCode")).orderQuery(inputParam);
        } catch (ClassNotFoundException e) {
            LogPortal.error("{} orderQuery 驱动类找不到",inputParam.getString("traceId"));
        }
        return null;
    }

    /**
     * 获取驱动类
     * @param channelCode
     * @return
     * @throws ClassNotFoundException
     */
    private BaseHandler getHandler(String channelCode) throws ClassNotFoundException {
        String beanName = channelCode.concat("Handler");
        return (BaseHandler) ApplicationContextProvider.getContext().getBean(beanName);
    }
}
